//
//  WJBasePopupView.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/20.
//

import UIKit

enum WJPopupAnimationStyle {
    case fromBottom
    case fromCenter
    case fromTop
}

class WJBasePopupView: UIView {
    
    // MARK: - Properties
    let contentView = UIView()
    private let backgroundView = UIView()
    var animationStyle: WJPopupAnimationStyle = .fromBottom
    private var isShowing = false
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    // MARK: - UI Setup
    private func setupUI() {
        // 背景视图
        backgroundView.backgroundColor = UIColor(hex: "#121212", alpha: 0.9)
        backgroundView.alpha = 0
        backgroundView.frame = bounds
        backgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(backgroundView)
        
        // 添加点击手势
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(backgroundTapped))
        backgroundView.addGestureRecognizer(tapGesture)
        
        // 内容视图
        contentView.backgroundColor = UIColor(hex: "#FFFFFF")
        contentView.layer.cornerRadius = 16
        contentView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        contentView.layer.masksToBounds = true
        addSubview(contentView)
    }
    
    // MARK: - Public Methods
    func show(in view: UIView? = nil, animationStyle: WJPopupAnimationStyle = .fromBottom) {
        guard !isShowing else { return }
        isShowing = true
        
        let targetView = view ?? (UIApplication.shared.currentWindow ?? UIView())
        frame = targetView.bounds
        targetView.addSubview(self)
        
        self.animationStyle = animationStyle
        setupContentViewPosition(for: animationStyle)
        
        // 动画显示
        UIView.animate(withDuration: 0.25) {
            self.backgroundView.alpha = 1.0
            self.setupContentViewFinalPosition()
        }
    }
    
    func dismiss(completion: (() -> Void)? = nil) {
        guard isShowing else { return }
        
        UIView.animate(withDuration: 0.25, animations: {
            self.backgroundView.alpha = 0
            self.setupContentViewInitialPosition()
        }) { _ in
            self.removeFromSuperview()
            self.isShowing = false
            completion?()
        }
    }
    
    // 添加内容到内容视图（子类重写）
    func setupContent() {
        // 子类重写此方法来自定义内容
    }
    
    // MARK: - Private Methods
    func setupContentViewPosition(for style: WJPopupAnimationStyle) {
        contentView.frame = CGRect(x: 0, y: 0, width: bounds.width - 40, height: 300)
        contentView.center.x = bounds.width / 2
        
        switch style {
        case .fromBottom:
            contentView.frame.origin.y = bounds.height
        case .fromCenter:
            contentView.center.y = bounds.height / 2
            contentView.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
            contentView.alpha = 0
        case .fromTop:
            contentView.frame.origin.y = -contentView.frame.height
        }
    }
    
    func setupContentViewFinalPosition() {
        switch animationStyle {
        case .fromBottom:
            contentView.frame.origin.y = bounds.height - contentView.frame.height - 20
        case .fromCenter:
            contentView.center.y = bounds.height / 2
            contentView.transform = .identity
            contentView.alpha = 1
        case .fromTop:
            contentView.frame.origin.y = 20
        }
    }
    
    private func setupContentViewInitialPosition() {
        switch animationStyle {
        case .fromBottom:
            contentView.frame.origin.y = bounds.height
        case .fromCenter:
            contentView.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
            contentView.alpha = 0
        case .fromTop:
            contentView.frame.origin.y = -contentView.frame.height
        }
    }
    
    // MARK: - Actions
    @objc private func backgroundTapped() {
        dismiss()
    }
    
    // MARK: - Layout
    override func layoutSubviews() {
        super.layoutSubviews()
        if isShowing {
            setupContentViewFinalPosition()
        }
    }
}
